package com.dormms.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dormms.bean.ProclamationInfo;
import com.dormms.dao.ProclamationDao;
import com.dormms.dto.ProclamationInfoRole;
import com.dormms.dto.ProclamationInfoRoleInfo;
import com.dormms.service.ProclamationService;

@Service
public class ProclamationServiceImpl implements ProclamationService {
	@Autowired
	private ProclamationDao proclamationDao;

	public List<ProclamationInfo> queryProclamationInfoListByProclamationInfo(ProclamationInfo proclamationInfo) {
		// TODO Auto-generated method stub
		return proclamationDao.queryProclamationInfo(proclamationInfo);
	}

	public Integer insertProclamationInfo(ProclamationInfo proclamationInfo) {
		// TODO Auto-generated method stub
		proclamationInfo.setProclamationCreate(new Date());
		return proclamationDao.insertProclamationInfo(proclamationInfo);
	}

	public Integer insertProclamationRole(ProclamationInfoRole proclamationInfoRole) {
		// TODO Auto-generated method stub
		Integer proclamationRoleId = proclamationDao.isExistProclamationInfoRole(proclamationInfoRole);
		if(proclamationRoleId != null) {
			return 0;
		}
		proclamationDao.insertProclamationInfoRole(proclamationInfoRole);
		return proclamationInfoRole.getProclamationRoleId();
	}

	public Integer updateProclamationInfo(ProclamationInfo proclamationInfo) {
		// TODO Auto-generated method stub
		return proclamationDao.updateProclamationInfo(proclamationInfo);
	}

	public ProclamationInfoRoleInfo queryProclamationInfoRoleInfoByProclamationId(Integer proclamationId) {
		// TODO Auto-generated method stub
		ProclamationInfoRoleInfo proclamationInfoRoleInfo = new ProclamationInfoRoleInfo();
		proclamationInfoRoleInfo.setProclamationInfo(proclamationDao.queryProclamationInfoByProclamationId(proclamationId));
		proclamationInfoRoleInfo.setRoles(proclamationDao.queryRoleInfoByProclamationId(proclamationId));
		return proclamationInfoRoleInfo;
	}

	public List<ProclamationInfo> queryAllProclamationInfoByRoleId(ProclamationInfo proclamationInfo,Integer roleId) {
		// TODO Auto-generated method stub
		return proclamationDao.queryAllProclamationInfoByRoleId(proclamationInfo,roleId);
	}

	public Integer deleteProclamation(Integer proclamationId) {
		// TODO Auto-generated method stub
		List<Integer> roleIds = proclamationDao.queryAllRoleIdByProclamationId(proclamationId);
		for (Integer roleId : roleIds) {
			proclamationDao.deleteProclamationRole(proclamationId,roleId);
		}
		return proclamationDao.deleteProclamation(proclamationId);
	}

	public Integer deleteProclamationRole(Integer proclamationId, Integer roleId) {
		// TODO Auto-generated method stub
		return proclamationDao.deleteProclamationRole(proclamationId,roleId);
	}

}
